#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <fcntl.h>
#include "kb_net.h"
#include "kb_def.h"


/*
 * open file of config
 */
static FILE* open_file(char * file_name)
{
	FILE *fp;

	fp = fopen(file_name, "r");
	if (fp < 0) {
		printf("file [%s] open error!\n", file_name);
		exit(1);
	}

	return fp;
}

/*
 * read file of *.cfg, fill struct TDeviceInfo.
 */
static int get_device_param(unsigned char *file, TDeviceInfo ** monitor)
{
	FILE *fp;	
	char buf[128];
	int count = 0, in = 0;
	char *pdata[32];
	char *pbuf;
	TDeviceInfo *pmonitor = *monitor;
	
	fp = open_file(file);
	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		memset(pdata, 0, sizeof(pdata));
		fscanf(fp, "%s", buf);
		pbuf = buf;
		in = 0;
		while ((pdata[in] = strtok(pbuf, ":")) != NULL) {
			in++;
			pbuf = NULL; 
		}											
		pmonitor[count].DeviceNo = atoi(pdata[0]);
		strcpy(pmonitor[count].IP  ,pdata[1]);
		pmonitor[count].Port = atoi(pdata[2]); 
		pmonitor[count].ChanNum = atoi(pdata[3]);
		strcpy(pmonitor[count].UserName, pdata[4]);
		strcpy(pmonitor[count].Password, pdata[5]);

		count++;
	}
	fclose(fp);

	return count;
}

/*
 * read file of cam.cfg, fill struct tcamera.
 */
static int get_camera_param(TDeviceInfo ** camera)
{
	FILE *fp;	
	char buf[128];
	int count = 0, in = 0;
	char *pdata[32];
	char *pbuf;
	TDeviceInfo *pcamera = *camera;
	
	fp = open_file(CAM_FILE);
	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		memset(pdata, 0, sizeof(pdata));
		fscanf(fp, "%s", buf);
		pbuf = buf;
		in = 0;

		while ((pdata[in] = strtok(pbuf, ":")) != NULL) {
			in++;
			pbuf = NULL; 
		}
											
		pcamera[count].DeviceNo = atoi(pdata[0]);
		strcpy(pcamera[count].IP  ,pdata[1]);
		pcamera[count].Port = atoi(pdata[2]);
		pcamera[count].ChanNum = atoi(pdata[3]);
		strcpy(pcamera[count].UserName, pdata[4]);
		strcpy(pcamera[count].Password, pdata[5]);
		
		count++;
	}
	fclose(fp);

	return count;
}

int fill_struct_monitor_and_camera(TDeviceInfo * monitor, TDeviceInfo * camera)
{
	//struct tmonitor	monitor[MONITOR_COUNT];
	//struct tcamera	camera[CAMERA_COUNT];
	struct TDeviceInfo	*pmonitor;
	struct TDeviceInfo  *pcamera;
	unsigned int count;

	memset(monitor, 0, sizeof(monitor));
	memset(camera, 0, sizeof(camera));

	pmonitor = monitor;
	pcamera = camera;

	count = get_monitor_param(&pmonitor);

	count = get_camera_param(&pcamera);
	

	return 0;
}

#if 0	/* DEBUG */
int fill_struct_monitor_and_camera_()
{
	struct tmonitor	monitor[MONITOR_COUNT];
	struct tcamera	camera[CAMERA_COUNT];
	struct tmonitor	*pmonitor;
	struct tcamera  *pcamera;
	unsigned int count, i;

	memset(monitor, 0, sizeof(monitor));
	memset(camera, 0, sizeof(camera));

	pmonitor = monitor;
	pcamera = camera;

	count = get_monitor_param(&pmonitor);
	#ifdef DEBUG
	for (i = 0; i < count; i++) {
		printf("monitor modid = %d\n", pmonitor[i].mon_id);
		printf("monitor ip = %s\n", pmonitor[i].ip);
		printf("monitor port = %d\n", pmonitor[i].port);
		printf("monitor chan_num = %d\n", pmonitor[i].chan_num);
		printf("monitor name = %s\n", pmonitor[i].name);
		printf("monitor pass = %s\n", pmonitor[i].pass);
		printf("-------------- next ---------------\n");
	}
	#endif

	//count = get_camera_param(&pcamera);
	#ifdef DEBUG
	for (i = 0; i < count; i++) {
		printf("camera cam_id = %d\n", pcamera[i].cam_id);
		printf("camera ip = %s\n", pcamera[i].ip);
		printf("camera port = %d\n", pcamera[i].port);
		printf("camera chan_num = %d\n", pcamera[i].chan_num);
		printf("camera name = %s\n", pcamera[i].name);
		printf("camera pass = %s\n", pcamera[i].pass);
		printf("-------------- next ---------------\n");
	}
	#endif

	return 0;
}

int main()
{
	while(1)
	fill_struct_monitor_and_camera_();
	return 0;
}

#endif


#if 0
#define NUMBER(bit, len, keycode)		(keycode[len-bit] - '0')
#define GET_VALUE(len, keycode)			(NUMBER(3, len, keycode) + \
										(NUMBER(4, len, keycode) * 10) + \
										(NUMBER(5, len, keycode) * 100) + \
										(NUMBER(6, len, keycode) * 1000))

#define GET_VALUE_THREE(len, keycode)	((keycode[len-3] - '0') + \
										((keycode[len-4] - '0')*10) + \
										((keycode[len-5] - '0')*100))

#define GET_VALUE_TWO(len, keycode)		((keycode[len-3] - '0') + \
										((keycode[len-4] - '0')*10))



static int get_command_string(unsigned int len, unsigned char * keycode)
{
	int i;
	char *pkeycode;

	if (len < 0 || keycode == NULL) {
		perror("key code was null!\n");
		return -1;
	}

	keycode_trace(LEVEL_3, "length is [%d]", len);

	#ifdef	NET
	for (i = 40; i < len; i++) {
	#else
	for (i = 0; i < len; i++) {
	#endif
		printf(" %02x", *(keycode + i));
	}
	pkeycode = keycode + CODE_POSITION;
//	handle_keycode(pkeycode);

	return 0;
}

static void view_keycode(unsigned int len, unsigned char * keycode)
{
	int i; 

	for (i = CODE_POSITION; i < len; i++) {
		printf("%02x ", *(keycode + i));
	}
	printf("\n");
	for (i = CODE_POSITION; i < len; i++) {
		printf("%c ", *(keycode + i));
	}
	printf("\n");
	keycode_trace(LEVEL_3, "\n\tEnd of view keycode!");
}

static int get_monitor(unsigned int len, unsigned char * keycode)
{
	int monitor_num;

	if (keycode[len-2] != 'M')
		return -1;

	// 个位monitor数
	if (len == 47) {
		monitor_num = keycode[len - 3] - '0';
	}
	// 十位monitor数
	else if (len == 48) {
		monitor_num = (keycode[len - 4] - '0') * 10 + (keycode[len - 3] - '0');
	}
	// 百位monitor数
	else if (len == 49) {
		monitor_num = (keycode[len - 5] - '0') * 100 + (keycode[len - 4] - '0') * 10 + (keycode[len - 3] - '0');
	} 
	else
		keycode_trace(LEVEL_8, "Error:monitor length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:monitor value is [%d]", monitor_num);

	return monitor_num;
}

static int get_camera(unsigned int len, unsigned char * keycode)
{
	int camrema_num;

	if (keycode[len-2] != '#')
		return -1;

	// 个位camera数
	if (len == 52) {
		camrema_num = keycode[len - 3] - '0';
	}
	// 十位camera数
	else if (len == 53) {
		camrema_num = (keycode[len - 4] - '0') * 10 + (keycode[len - 3] - '0');
	}
	// 百位camera数
	else if (len == 54) {
		camrema_num = (keycode[len - 5] - '0') * 100 + (keycode[len - 4] - '0') * 10 + (keycode[len - 3] - '0');
	} 
	// 千位camera数
	else if (len == 55) {
		camrema_num = (keycode[len - 6] - '0') * 1000 + (keycode[len - 5] - '0') * 100  + 
					  (keycode[len - 4] - '0') * 10   + (keycode[len - 3] - '0');
	} 
	else
		keycode_trace(LEVEL_8, "Error:camera length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:camrema value is [%d]", camrema_num);

	return camrema_num;
}

static int shot_set(unsigned int len, unsigned char * keycode)
{
	int set_value;

	if (len == 52) {
		set_value = NUMBER(3,len,keycode);
	}
	else if (len == 53) {
		set_value = NUMBER(3,len,keycode) + NUMBER(4,len,keycode) * 10;
	}
	else if (len == 54) {
		set_value = GET_VALUE_THREE(len, keycode);
	} 
	else if (len == 55) {
		set_value = GET_VALUE(len, keycode);
	} 
	else
		keycode_trace(LEVEL_8, "Error:shot set length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:shot set value is [%d]", set_value);

	return set_value;
}

static int shot_call(unsigned int len, unsigned char * keycode)
{
	int call_value;

	if (len == 52) {
		call_value = NUMBER(3,len,keycode);
	}
	else if (len == 53) {
		call_value = GET_VALUE_TWO(len, keycode);
	}
	else if (len == 54) {
		call_value = GET_VALUE_THREE(len, keycode);
	} 
	else if (len == 55) {
		call_value = GET_VALUE(len, keycode);
	} 
	else
		keycode_trace(LEVEL_8, "Error:shot call length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:shot call value is [%d]", call_value);

	return call_value;
}

static int run(unsigned int len, unsigned char * keycode)
{
	int run_value;

	if (len == 52) {
		run_value = NUMBER(3,len,keycode);
	}
	else if (len == 53) {
		run_value = GET_VALUE_TWO(len, keycode);
	}
	else if (len == 54) {
		run_value = GET_VALUE_THREE(len, keycode);
	} 
	else if (len == 55) {
		run_value = GET_VALUE(len, keycode);
	} 
	else
		keycode_trace(LEVEL_8, "Error:run length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:run value is [%d]", run_value);

	return run_value;
}

static int hold(unsigned int len, unsigned char * keycode)
{
	int hold_value;

	if (len == 52) {
		hold_value = NUMBER(3,len,keycode);
	}
	else if (len == 53) {
		hold_value = GET_VALUE_TWO(len, keycode);
	}
	else if (len == 54) {
		hold_value = GET_VALUE_THREE(len, keycode);
	} 
	else if (len == 55) {
		hold_value = GET_VALUE(len, keycode);
	} 
	else
		keycode_trace(LEVEL_8, "Error:hold length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:hold value is [%d]", hold_value);

	return hold_value;
}

static int salvo(unsigned int len, unsigned char * keycode)
{
	int salvo_value;

	if (len == 52) {
		salvo_value = NUMBER(3,len,keycode);
	}
	else if (len == 53) {
		salvo_value = GET_VALUE_TWO(len, keycode);
	}
	else if (len == 54) {
		salvo_value = GET_VALUE_THREE(len, keycode);
	} 
	else if (len == 55) {
		salvo_value = GET_VALUE(len, keycode);
	} 
	else
		keycode_trace(LEVEL_8, "Error:salvo length is [%d]", len);

	keycode_trace(LEVEL_8, "Result:salvo value is [%d]", salvo_value);

	return salvo_value;
}

static int up_operate(unsigned int len, unsigned char * keycode)
{
	int up_val;

	up_val = NUMBER(3, len, keycode);
	keycode_trace(LEVEL_8, "Result:up_val value is [%d]", up_val);

	return up_val;
}

static int down_operate(unsigned int len, unsigned char * keycode)
{
	int down_val;
	
	down_val = NUMBER(3, len, keycode);
	keycode_trace(LEVEL_8, "Result:down_val value is [%d]", down_val);

	return down_val;
}

static int left_operate(unsigned int len, unsigned char * keycode)
{
	int left_val;

	left_val = NUMBER(3, len, keycode);
	keycode_trace(LEVEL_8, "Result:left_val value is [%d]", left_val);

	return left_val;
}

static int right_operate(unsigned int len, unsigned char * keycode)
{
	int right_val;

	right_val = NUMBER(3, len, keycode);
	keycode_trace(LEVEL_8, "Result:right_val value is [%d]", right_val);

	return right_val;
}


#ifdef DEBUG
		printf("monitor modid = %d\n", pcamera[count].cam_id);
		printf("monitor ip = %s\n", pcamera[count].ip);
		printf("monitor port = %d\n", pcamera[count].port);
		printf("monitor chan_num = %d\n", pcamera[count].chan_num);
		printf("monitor name = %s\n", pcamera[count].name);
		printf("monitor pass = %s\n", pcamera[count].pass);
#endif

static int get_camera_param(struct tcamera ** camera)
{
	FILE *fp;	
	char buf[128];
	int count = 0, in = 0;
	char *pdata[32];
	char *pbuf;
	struct tcamera *pcamera = *camera;
	
	fp = open_file(CAM_FILE);
	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		memset(pdata, 0, sizeof(pdata));
		fscanf(fp, "%s", buf);
		pbuf = buf;
		in = 0;
		#ifdef DEBUG
		printf("read mon.cfg line [%d] = %s\n", count, buf);
		#endif
		while ((pdata[in] = strtok(pbuf, ":")) != NULL) {
			#ifdef DEBUG
			printf("%s\n", pdata[in]);
			#endif
			in++;
			pbuf = NULL; 
		}
											
		pcamera[count].cam_id = atoi(pdata[0]);
		strcpy(pcamera[count].ip  ,pdata[1]);
		pcamera[count].port = atoi(pdata[2]);
		pcamera[count].chan_num = atoi(pdata[3]);
		strcpy(pcamera[count].name, pdata[4]);
		strcpy(pcamera[count].pass, pdata[5]);
		
		count++;
	}
	fclose(fp);

	return count;
}

#endif
